rm(list = ls())
library(data.table)
library(plyr)
library(tidyr)
library(lfe)
library(stargazer)
library(xtable)
library(sandwich)
library(roll)
library(readxl)
library(readr)
library(zoo)
library(texreg)
library(DescTools)
library(ggplot2)

m <- data.table(read_xlsx("../Data/MasterData.xlsx", skip = 1, guess_max = 1e4))
m[, age := 2021 - year]

## merge in SH daily index
sh <- data.table(read_xlsx("../Data/SHIndex.xlsx", guess_max = 1e4))
sh <- sh[, .(Idxtrd01, Idxtrd05)]
names(sh) <- c("date", "SHindex")
sh <- sh[-1]
sh[, date := as.Date(date)]
setorder(sh, date)

sh[, ym := as.yearmon(date)]
sh <- sh[, .(SHindex = SHindex[.N]), by = .(ym)]
sh[, SHindex := as.numeric(SHindex)]
sh[, SHindex := SHindex]

m[, memrableperiod_begin := as.yearmon(memrableperiod_begin, "%Ym%m")]
m[, memrableperiod_end := as.yearmon(memrableperiod_end, "%Ym%m")]

C <- data.table(read_xlsx("../Data/AccountData.xlsx", guess_max = 1e4))

C[, netbuy_tday_21 := as.numeric(netbuy_tday_21)]
C[, asize_tday_1 := as.numeric(asize_tday_1)]
C[, asize_tday_22 := as.numeric(asize_tday_22)]
C[, monthly_turnover_tsa := as.numeric(monthly_turnover_tsa)]

m <- merge(m, C,
  by = "id", all.x = F
)

m[, trading := monthly_turnover_tsa]
m[is.infinite(trading), trading := NA]

m[, trading := Winsorize(trading, quantile(trading, probs = c(0.05, 0.95), na.rm = T))]
m[, netbuy_tday_21 := Winsorize(netbuy_tday_21, quantile(netbuy_tday_21, probs = c(0.05, 0.95), na.rm = T))]
m[, asize_tday_1 := Winsorize(asize_tday_1, quantile(asize_tday_1, probs = c(0.05, 0.95), na.rm = T))]
m[, asize_tday_22 := Winsorize(asize_tday_22, quantile(asize_tday_22, probs = c(0.05, 0.95), na.rm = T))]

m <- m[!is.na(trading)]

m[netbuy_tday_21 >= 0, .(mean(memrableperiod_begin >= "Jan 2021"))]
m[netbuy_tday_21 < 0, .(mean(memrableperiod_begin >= "Jan 2021"))]

###########################################################################
# Distribution of recalled episodes
###########################################################################

m <- m[type == 0]

## Figure 
tmp <- table(m[trading >= median(trading)]$memrableperiod_begin) * 100
C <- data.table(tmp)
names(C) <- c("ym", "count")
C[, ym := as.yearmon(ym)]
setorder(C, ym)
C <- merge(C, sh, by = c("ym"), all.y = T)

max(C$count, na.rm = T)
ggplot(C) +
  xlab("") +
  ylab("") +
  geom_bar(aes(x = ym, y = count), stat = "identity") +
  geom_line(aes(x = ym, y = SHindex), color = "blue") +
  theme_minimal() +
  ylim(c(0, 8000)) +
  scale_x_continuous(expand = c(0, 0), limits = c(1992, 2022))
ggsave("../Figures/pic-buypositive-datestart.pdf", width = 6, height = 4)

tmp <- table(m[trading >= median(trading)]$memrableperiod_end) * 100
C <- data.table(tmp)
names(C) <- c("ym", "count")
C[, ym := as.yearmon(ym)]
setorder(C, ym)
C <- merge(C, sh, by = c("ym"), all.y = T)

max(C$count, na.rm = T)
ggplot(C) +
  xlab("") +
  ylab("") +
  geom_bar(aes(x = ym, y = count), stat = "identity") +
  geom_line(aes(x = ym, y = SHindex), color = "blue") +
  theme_minimal() +
  ylim(c(0, 8000)) +
  scale_x_continuous(expand = c(0, 0), limits = c(1992, 2022))
ggsave("../Figures/pic-buypositive-dateend.pdf", width = 6, height = 4)

## Figure
tmp <- table(m[trading < median(trading)]$memrableperiod_begin) * 100
C <- data.table(tmp)
names(C) <- c("ym", "count")
C[, ym := as.yearmon(ym)]
setorder(C, ym)
C <- merge(C, sh, by = c("ym"), all.y = T)

max(C$count, na.rm = T)
ggplot(C) +
  xlab("") +
  ylab("") +
  geom_bar(aes(x = ym, y = count), stat = "identity") +
  geom_line(aes(x = ym, y = SHindex), color = "blue") +
  theme_minimal() +
  ylim(c(0, 8000)) +
  scale_x_continuous(expand = c(0, 0), limits = c(1992, 2022))
ggsave("../Figures/pic-buynegative-datestart.pdf", width = 6, height = 4)

tmp <- table(m[trading < median(trading)]$memrableperiod_end) * 100
C <- data.table(tmp)
names(C) <- c("ym", "count")
C[, ym := as.yearmon(ym)]
setorder(C, ym)
C <- merge(C, sh, by = c("ym"), all.y = T)

max(C$count, na.rm = T)
ggplot(C) +
  xlab("") +
  ylab("") +
  geom_bar(aes(x = ym, y = count), stat = "identity") +
  geom_line(aes(x = ym, y = SHindex), color = "blue") +
  theme_minimal() +
  ylim(c(0, 8000)) +
  scale_x_continuous(expand = c(0, 0), limits = c(1992, 2022))
ggsave("../Figures/pic-buynegative-dateend.pdf", width = 6, height = 4)
